def binary_search(sorted_array, val) -> int:
    if not sorted_array:
        return -1
    beg = 0
    end = len(sorted_array) - 1
    while beg <= end:
        mid = (beg+end)//2
        if sorted_array[mid] == val:
            return mid
        elif sorted_array[mid] > val:
            end = mid - 1
        else:
            beg = mid + 1
    return -1

def test_binary_search():
    a = list(range(10))
    # 正常值
    assert binary_search(a, 1) == 1
    assert binary_search(a, -1) == -1
    # 异常值
    assert binary_search(None, 1) == -1
    # 边界值
    assert binary_search(a, 0) == 0

binary_search(list(range(10)),5)==1